package cn.yunsenlin.formula.cloud.share;

import cn.yunsenlin.formula.cloud.interfaces.ShareClassificationService;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.*;
import cn.yunsenlin.formula.cloud.mysql.mapper.generator.TbTemplateClassificationMapper;
import cn.yunsenlin.formula.cloud.mysql.mapper.generator.TbTemplateLinkClassificationMapper;
import cn.yunsenlin.formula.cloud.mysql.mapper.generator.TbTemplateMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShareClassificationServiceImpl implements ShareClassificationService {

    private final TbTemplateClassificationMapper tbTemplateClassificationMapper;
    private final TbTemplateLinkClassificationMapper tbTemplateLinkClassificationMapper;
    private final TbTemplateMapper tbTemplateMapper;

    @SuppressWarnings("all")
    public ShareClassificationServiceImpl(TbTemplateClassificationMapper tbTemplateClassificationMapper, TbTemplateLinkClassificationMapper tbTemplateLinkClassificationMapper, TbTemplateMapper tbTemplateMapper) {
        this.tbTemplateClassificationMapper = tbTemplateClassificationMapper;
        this.tbTemplateLinkClassificationMapper = tbTemplateLinkClassificationMapper;
        this.tbTemplateMapper = tbTemplateMapper;
    }

    @Override
    public List<TbTemplateClassification> selectRootClassificationList() {
        TbTemplateClassificationCriteria criteria = new TbTemplateClassificationCriteria();
        criteria.or().andParentidIsNull();
        return tbTemplateClassificationMapper.selectByExample(criteria);
    }

    @Override
    public List<TbTemplate> selectTemplateListByClassification(int classificationId) {
        List<TbTemplate> tbTemplates = new ArrayList<>();
        TbTemplateLinkClassificationCriteria criteria = new TbTemplateLinkClassificationCriteria();
        criteria.or().andClassificationIdEqualTo(classificationId);
        criteria.setOrderByClause("rank asc");
        tbTemplateLinkClassificationMapper.selectByExample(criteria).forEach(link -> {
            TbTemplate tbTemplate = new TbTemplate();
            TbTemplate db = tbTemplateMapper.selectByPrimaryKey(link.getTemplateId());
            tbTemplate.setShareUuid(db.getShareUuid());
            tbTemplate.setName(db.getName());
            tbTemplates.add(tbTemplate);
        });
        return tbTemplates;
    }
}
